﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
//using YArchitech.Plumbing.Model;
using YArchitech.Revit.RevitExtension;
using YJKArchUtils.Utils;
using YJKRevitKernel.RevitDimensionUtility;
using YJKGeometryFuncs;
using YJKRevitDimensionText.DimCreator;
using YJKRevitDimensionText.OneKeyPlanDim.DimesionDataEx;
using YJKRevitDimensionText.OneKeyPlanDim.Utility.XDataUtils;

namespace YJKRevitDimensionText.OneKeyPlanDim.InWallDim
{
	public class DrawInWallThickDim
	{
		public DrawInWallThickDim(Autodesk.Revit.DB.Document doc, BaseDataOper baseData, List<ElementId> lstCrossWallId, Dictionary<ElementId, ElementId> dicWallDim)
		{
			this.m_Doc = doc;
			this.m_BaseDataOper = baseData;
			this.m_LstAllWallDataEx = this.m_BaseDataOper.GetInWallData();
			this.m_LstDimensionedWall = lstCrossWallId;
			this.m_DicWallDim = dicWallDim;
			this.DeleteOutWallDim(dicWallDim);
		}

		private void DeleteOutWallDim(Dictionary<ElementId, ElementId> dicWallDim)
		{
			Transaction transaction = new Transaction(this.m_Doc, "删除外墙标注");
			transaction.Start();
			using (Dictionary<ElementId, ElementId>.KeyCollection.Enumerator enumerator = dicWallDim.Keys.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ElementId eId = enumerator.Current;
					if (this.m_LstAllWallDataEx.Exists((WallDataEx w) => w.WallId.ToString() == eId.ToString()))
					{
						this.m_Doc.Delete(dicWallDim[eId]);
					}
				}
			}
			transaction.Commit();
			List<WallDataEx> list = new List<WallDataEx>();
			foreach (WallDataEx wallDataEx in this.m_LstAllWallDataEx)
			{
				if (wallDataEx.OutLine is Line)
				{
					list.Add(wallDataEx);
				}
			}
			this.m_LstAllWallDataEx = list;
		}

		private void CollinearInWallAndOutWall(WallDataEx wallInDataEx)
		{
			List<WallDataEx> outWallData = this.m_BaseDataOper.GetOutWallData();
			foreach (ElementId elementId in this.m_DicWallDim.Keys)
			{
				foreach (WallDataEx wallDataEx in outWallData)
				{
					if (wallDataEx.WallId.ToString() == elementId.ToString() && (this.IsTwoLineCollineation(wallInDataEx.InLine, wallDataEx.InLine) || this.IsTwoLineCollineation(wallInDataEx.InLine, wallDataEx.OutLine)))
					{
						try
						{
							if (Math.Abs(wallInDataEx.WallWidth - wallDataEx.WallWidth) < 0.001)
							{
								this.m_Doc.Delete(this.m_DicWallDim[elementId]);
							}
						}
						catch
						{
						}
					}
				}
			}
		}

		public void DrawDim()
		{
			Dictionary<string, List<WallDataEx>> dictionary = new Dictionary<string, List<WallDataEx>>();
			List<WallDataEx> list = new List<WallDataEx>();
			List<GridGroupDataEx> allParallelGrid = this.m_BaseDataOper.GetAllParallelGrid();
			foreach (WallDataEx wallDataEx in this.m_LstAllWallDataEx)
			{
				Grid grid = null;
				if (wallDataEx.InLine is Line && this.DistancePoint(wallDataEx.InLine.GetEndPoint(0), wallDataEx.InLine.GetEndPoint(1)) > 3.0)
				{
					foreach (GridGroupDataEx gridGroupDataEx in allParallelGrid)
					{
						if (this.IsParallel(wallDataEx, gridGroupDataEx))
						{
							grid = this.FindNearestLine(wallDataEx.InLine as Line, gridGroupDataEx);
							if (grid != null)
							{
								if (dictionary.ContainsKey(grid.Curve.Reference.ElementId.ToString()))
								{
									dictionary[grid.Curve.Reference.ElementId.ToString()].Add(wallDataEx);
								}
								else
								{
									dictionary.Add(grid.Curve.Reference.ElementId.ToString(), new List<WallDataEx>
									{
										wallDataEx
									});
									this.m_DicGrid.Add(grid.Curve.Reference.ElementId.ToString(), grid);
								}
							}
						}
					}
					if (grid == null)
					{
						list.Add(wallDataEx);
					}
				}
			}
			this.MergeWall(list, dictionary);
			this.TryAutoAvoid();
		}

		private void TryAutoAvoid()
		{
			Transaction transaction = new Transaction(this.m_Doc, "标注写入扩展数据");
			foreach (Dimension dimension in this.m_LstDim)
			{
				DimAvoidManager.ClearCache();
				DimAvoidManager.TryAutoAvoidForHorizontal(this.m_Doc, dimension);
				transaction.Start();
				ElementStringXDataUtil.WriteOneKeyPlaneDimMark(dimension);
				transaction.Commit();
			}
			DimAvoidManager.TryAutoAvoidElementForHorizontal(this.m_Doc, this.m_LstDim);
		}

		private List<WallDataEx> GetFilterWallDataEx()
		{
			List<WallDataEx> list = new List<WallDataEx>();
			using (List<WallDataEx>.Enumerator enumerator = this.m_LstAllWallDataEx.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					WallDataEx wallDataEx = enumerator.Current;
					if (!this.m_LstDimensionedWall.Exists((ElementId w) => w.ToString() == wallDataEx.WallId.ToString()))
					{
						list.Add(wallDataEx);
					}
				}
			}
			return list;
		}

		private void MergeWall(List<WallDataEx> lstNoGrid, Dictionary<string, List<WallDataEx>> dicGroup)
		{
			lstNoGrid = this.MergeWall(lstNoGrid);
			for (int i = 0; i < dicGroup.Keys.Count; i++)
			{
				dicGroup[dicGroup.Keys.ElementAt(i)] = this.MergeWall(dicGroup[dicGroup.Keys.ElementAt(i)]);
			}
			Transaction transaction = new Transaction(this.m_Doc, "创建标注");
			transaction.Start();
			this.GroupByOverlap(dicGroup);
			foreach (WallDataEx wallDataEx in lstNoGrid)
			{
				this.CreateDimension(wallDataEx);
			}
			transaction.Commit();
		}

		private void GroupByOverlap(Dictionary<string, List<WallDataEx>> dicGroup)
		{
			foreach (string key in dicGroup.Keys)
			{
				if (dicGroup[key].Count > 0)
				{
					Grid grid = this.m_DicGrid[key];
					List<ProPtEx> list = new List<ProPtEx>();
					XYZ vectorByGrid = this.GetVectorByGrid(grid.Curve as Line);
					foreach (WallDataEx wallDataEx in dicGroup[key])
					{
						if (vectorByGrid.Normalize().IsAlmostEqualTo((wallDataEx.InLine.GetEndPoint(1) - wallDataEx.InLine.GetEndPoint(0)).Normalize(), 0.0001))
						{
							list.Add(new ProPtEx(this.GetFootPoint(grid.Curve.GetEndPoint(0), grid.Curve.GetEndPoint(1), wallDataEx.InLine.GetEndPoint(0)), wallDataEx, true));
							list.Add(new ProPtEx(this.GetFootPoint(grid.Curve.GetEndPoint(0), grid.Curve.GetEndPoint(1), wallDataEx.InLine.GetEndPoint(1)), wallDataEx, false));
						}
						else
						{
							list.Add(new ProPtEx(this.GetFootPoint(grid.Curve.GetEndPoint(0), grid.Curve.GetEndPoint(1), wallDataEx.InLine.GetEndPoint(0)), wallDataEx, false));
							list.Add(new ProPtEx(this.GetFootPoint(grid.Curve.GetEndPoint(0), grid.Curve.GetEndPoint(1), wallDataEx.InLine.GetEndPoint(1)), wallDataEx, true));
						}
					}
					list = this.SortProPtEx(list, grid.Curve.GetEndPoint(0), grid.Curve.GetEndPoint(1));
					List<ProSectionEx> allSections = ProSectionEx.GetAllSections(list);
					this.SortProPtEx(allSections);
					this.FindFurthestTowElements(allSections, grid);
				}
			}
		}

		private XYZ GetVectorByGrid(Line gridLine)
		{
			if (Math.Abs(gridLine.GetEndPoint(1).Y - gridLine.GetEndPoint(0).Y) < 0.0001)
			{
				if (gridLine.GetEndPoint(0).X > gridLine.GetEndPoint(1).X)
				{
					return gridLine.GetEndPoint(0) - gridLine.GetEndPoint(1);
				}
				return gridLine.GetEndPoint(1) - gridLine.GetEndPoint(0);
			}
			else
			{
				if (gridLine.GetEndPoint(0).Y > gridLine.GetEndPoint(1).Y)
				{
					return gridLine.GetEndPoint(0) - gridLine.GetEndPoint(1);
				}
				return gridLine.GetEndPoint(1) - gridLine.GetEndPoint(0);
			}
		}

		private void SortProPtEx(List<ProSectionEx> lstProSectionEx)
		{
			for (int i = 0; i < lstProSectionEx.Count - 1; i++)
			{
				for (int j = i + 1; j < lstProSectionEx.Count; j++)
				{
					if (lstProSectionEx[i].LstObject.Count < lstProSectionEx[j].LstObject.Count)
					{
						ProSectionEx value = lstProSectionEx[i];
						lstProSectionEx[i] = lstProSectionEx[j];
						lstProSectionEx[j] = value;
					}
				}
			}
		}

		private List<ProPtEx> SortProPtEx(List<ProPtEx> lstProPtEx, XYZ startPoint, XYZ endPoint)
		{
			List<ProPtEx> result = new List<ProPtEx>();
			if (Math.Abs(startPoint.Y - endPoint.Y) < 0.0001)
			{
				result = (from o in lstProPtEx
				orderby o.ProjectPt.X
				select o).ToList<ProPtEx>();
			}
			else
			{
				result = (from o in lstProPtEx
				orderby o.ProjectPt.Y
				select o).ToList<ProPtEx>();
			}
			return result;
		}

		private void FindFurthestTowElements(List<ProSectionEx> lstProSectionEx, Grid grid)
		{
			foreach (ProSectionEx proSectionEx in lstProSectionEx)
			{
				if (proSectionEx.SectionLine != null)
				{
					List<WallDataEx> list = new List<WallDataEx>();
					list = proSectionEx.LstObject.ConvertAll<WallDataEx>((object input) => input as WallDataEx);
					if (list.Count > 1)
					{
						Line line = null;
						Line line2 = null;
						if (this.IsOneSide(grid, list, ref line, ref line2))
						{
							if (line != null)
							{
								this.CreateDimension(list, line, grid.Curve, grid.Curve, proSectionEx.SectionLine, true);
							}
							else if (line2 != null)
							{
								this.CreateDimension(list, line2, grid.Curve, grid.Curve, proSectionEx.SectionLine, true);
							}
						}
						else
						{
							this.CreateDimension(list, line, line2, grid.Curve, proSectionEx.SectionLine, false);
						}
					}
					else if (list.Count > 0 && !this.m_DeleteWallId.Contains(list[0].WallId.ToString()))
					{
						this.CreateOneWallDim(list, grid, proSectionEx);
					}
				}
			}
		}

		private void CreateOneWallDim(List<WallDataEx> lstWallDataEx, Grid grid, ProSectionEx proSectionEx)
		{
			XYZ footPoint = this.GetFootPoint(lstWallDataEx[0].InLine.GetEndPoint(0), lstWallDataEx[0].InLine.GetEndPoint(1), grid.Curve.GetEndPoint(0));
			XYZ footPoint2 = this.GetFootPoint(lstWallDataEx[0].OutLine.GetEndPoint(0), lstWallDataEx[0].OutLine.GetEndPoint(1), grid.Curve.GetEndPoint(0));
			double num = this.DistancePoint(grid.Curve.GetEndPoint(0), footPoint);
			double num2 = this.DistancePoint(grid.Curve.GetEndPoint(0), footPoint2);
			if (num > num2)
			{
				this.CreateDimension(lstWallDataEx, lstWallDataEx[0].InLine, grid.Curve, grid.Curve, proSectionEx.SectionLine, true);
				return;
			}
			this.CreateDimension(lstWallDataEx, lstWallDataEx[0].OutLine, grid.Curve, grid.Curve, proSectionEx.SectionLine, true);
		}

		private void CreateDimension(List<WallDataEx> lstWallDataEx, Curve markLineFirst, Curve markLineSecond, Curve gridCurve, Line projectionLine, bool isGrid)
		{
			XYZ xyz = new XYZ((projectionLine.GetEndPoint(0).X + projectionLine.GetEndPoint(1).X) / 2.0, (projectionLine.GetEndPoint(0).Y + projectionLine.GetEndPoint(1).Y) / 2.0, 0.0);
			XYZ footPoint = this.GetFootPoint(markLineFirst.GetEndPoint(0), markLineFirst.GetEndPoint(1), xyz);
			XYZ xyz2;
			if (isGrid)
			{
				xyz2 = xyz;
			}
			else
			{
				xyz2 = this.GetFootPoint(markLineSecond.GetEndPoint(0), markLineSecond.GetEndPoint(1), xyz);
			}
			xyz2 = new XYZ(xyz2.X, xyz2.Y, footPoint.Z);
			Line line = YJKLineEx.YJKGetBound(footPoint, xyz2);
			ReferenceArray referenceArray = new ReferenceArray();
			List<string> list = new List<string>();
			new List<DetailCurve>();
			foreach (WallDataEx wallDataEx in lstWallDataEx)
			{
				if (!this.m_DeleteWallId.Contains(wallDataEx.WallId.ToString()))
				{
					this.CollinearInWallAndOutWall(wallDataEx);
					if (!this.IsTwoLineCollineation(gridCurve, wallDataEx.InLine))
					{
						XYZ intersection = this.GetIntersection(wallDataEx.InLine.Clone() as Line, line.Clone() as Line);
						referenceArray.Append(this.CreateShortLine(wallDataEx.InLine as Line, intersection));
					}
					if (!this.IsTwoLineCollineation(gridCurve, wallDataEx.OutLine))
					{
						XYZ intersection2 = this.GetIntersection(wallDataEx.OutLine.Clone() as Line, line.Clone() as Line);
						referenceArray.Append(this.CreateShortLine(wallDataEx.OutLine as Line, intersection2));
					}
					this.m_DeleteWallId.Add(wallDataEx.WallId.ToString());
					list.Add(wallDataEx.WallId.ToString());
				}
			}
			Line line2 = Line.CreateBound(new XYZ(gridCurve.GetEndPoint(0).X, gridCurve.GetEndPoint(0).Y, footPoint.Z), new XYZ(gridCurve.GetEndPoint(1).X, gridCurve.GetEndPoint(1).Y, footPoint.Z));
			XYZ intersection3 = this.GetIntersection(line2.Clone() as Line, line.Clone() as Line);
			referenceArray.Append(this.CreateShortLine(line2, intersection3));
			this.GetIntersectsWall(line, referenceArray, lstWallDataEx, list);
			referenceArray = this.DuplicateRemoval(referenceArray);
			DimensionType dimensionType = RevitDimensionUtiltyMethod.GetDimensionType(this.m_Doc, 0, null);
			try
			{
				this.m_LstDim.Add(this.m_Doc.Create.NewDimension(this.m_Doc.ActiveView, line, referenceArray, dimensionType));
			}
			catch
			{
			}
		}

		private ReferenceArray DuplicateRemoval(ReferenceArray wallRefArr)
		{
			ReferenceArray referenceArray = new ReferenceArray();
			List<string> list = new List<string>();
			for (int i = 0; i < wallRefArr.Size; i++)
			{
				try
				{
                    if (!list.Contains(wallRefArr.get_Item(i).ElementId.ToString()))
					{
                        DetailCurve detailCurve = this.m_Doc.GetElement(wallRefArr.get_Item(i).ElementId) as DetailCurve;
						for (int j = i + 1; j < wallRefArr.Size; j++)
						{
							try
							{
                                DetailCurve detailCurve2 = this.m_Doc.GetElement(wallRefArr.get_Item(j).ElementId) as DetailCurve;
								if (this.IsTwoLineCollineation(detailCurve.GeometryCurve, detailCurve2.GeometryCurve))
								{
									list.Add(detailCurve2.Id.ToString());
								}
							}
							catch
							{
							}
						}
						referenceArray.Append(new Reference(detailCurve));
					}
				}
				catch
				{
				}
			}
			return referenceArray;
		}

		private bool IsTwoLineCollineation(Curve curveOne, Curve curveTwo)
		{
			XYZ xyz = curveOne.GetEndPoint(1) - curveOne.GetEndPoint(0);
			XYZ xyz2 = curveTwo.GetEndPoint(1) - curveTwo.GetEndPoint(0);
			return Math.Abs(this.DistancePoint(curveTwo.GetEndPoint(0), this.GetFootPoint(curveOne.GetEndPoint(1), curveOne.GetEndPoint(0), curveTwo.GetEndPoint(0)))) < 0.001 && Math.Abs(xyz.X * xyz2.Y - xyz2.X * xyz.Y) < 0.0001;
		}

		private void GetIntersectsWall(Line dimLine, ReferenceArray wallRefArr, List<WallDataEx> lstWallDataEx, List<string> lsId)
		{
			foreach (WallDataEx wallDataEx in lstWallDataEx)
			{
				if (!lsId.Contains(wallDataEx.WallId.ToString()))
				{
					this.CollinearInWallAndOutWall(wallDataEx);
					XYZ intersection = this.GetIntersection(dimLine.Clone() as Line, wallDataEx.InLine.Clone() as Line);
                    if (intersection != null && YJKArch.Utils.Geometry.Is_Point_OnSegment(dimLine.GetEndPoint(0), dimLine.GetEndPoint(1), intersection, 1E-09))
					{
						wallRefArr.Append(this.CreateShortLine(wallDataEx.InLine as Line, intersection));
					}
					XYZ intersection2 = this.GetIntersection(dimLine.Clone() as Line, wallDataEx.OutLine.Clone() as Line);
                    if (intersection2 != null && YJKArch.Utils.Geometry.Is_Point_OnSegment(dimLine.GetEndPoint(0), dimLine.GetEndPoint(1), intersection2, 1E-09))
					{
						wallRefArr.Append(this.CreateShortLine(wallDataEx.OutLine as Line, intersection2));
					}
				}
			}
		}

		private void CreateDimension(WallDataEx wallDataEx)
		{
			this.CollinearInWallAndOutWall(wallDataEx);
			XYZ xyz = new XYZ((wallDataEx.OutLine.GetEndPoint(0).X + wallDataEx.OutLine.GetEndPoint(1).X) / 2.0, (wallDataEx.OutLine.GetEndPoint(0).Y + wallDataEx.OutLine.GetEndPoint(1).Y) / 2.0, 0.0);
			XYZ footPoint = this.GetFootPoint(wallDataEx.InLine.GetEndPoint(0), wallDataEx.InLine.GetEndPoint(1), xyz);
			Line line = YJKLineEx.YJKGetBound(footPoint, xyz);
			ReferenceArray referenceArray = new ReferenceArray();
			referenceArray.Append(this.CreateShortLine(wallDataEx.InLine as Line, footPoint));
			referenceArray.Append(this.CreateShortLine(wallDataEx.OutLine as Line, xyz));
			DimensionType dimensionType = RevitDimensionUtiltyMethod.GetDimensionType(this.m_Doc, 0, null);
			try
			{
				this.m_LstDim.Add(this.m_Doc.Create.NewDimension(this.m_Doc.ActiveView, line, referenceArray, dimensionType));
			}
			catch
			{
			}
		}

		private Reference CreateShortLine(Line refLine, XYZ startPoint)
		{
			Reference result;
			try
			{
				XYZ dir = startPoint - refLine.GetEndPoint(0);
				result = new Reference(this.GetDetailCurveWithPoint(this.m_Doc, startPoint, dir));
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private DetailCurve GetDetailCurveWithPoint(Autodesk.Revit.DB.Document doc, XYZ startPoint, XYZ dir)
		{
			XYZ ptEnd = startPoint + dir.Normalize() * Common.MMToFeet(10.0);
			return this.CreateDetailCurve(doc, startPoint, ptEnd);
		}

		private DetailCurve CreateDetailCurve(Autodesk.Revit.DB.Document doc, XYZ ptStart, XYZ ptEnd)
		{
			DetailCurve result;
			try
			{
				Line line = Line.CreateBound(new XYZ(ptStart.X, ptStart.Y, this.m_Doc.ActiveView.GenLevel.Elevation), new XYZ(ptEnd.X, ptEnd.Y, this.m_Doc.ActiveView.GenLevel.Elevation));
				result = doc.Create.NewDetailCurve(doc.ActiveView, line);
			}
			catch (Exception)
			{
				result = null;
			}
			return result;
		}

		private XYZ GetIntersection(Line lineOne, Line lineTwo)
		{
			lineOne.MakeUnbound();
			lineTwo.MakeUnbound();
			IntersectionResultArray intersectionResultArray;
            if ((int)lineOne.Intersect(lineTwo, out intersectionResultArray) != 8)
			{
				return null;
			}
			if (intersectionResultArray == null || intersectionResultArray.Size != 1)
			{
				return null;
			}
			return intersectionResultArray.get_Item(0).XYZPoint;
		}

		private bool IsOneSide(Grid grid, List<WallDataEx> lstWallDataEx, ref Line oneSide, ref Line anotherSide)
		{
			double num = -1.0;
			double num2 = -1.0;
			this.GetFootPoint(lstWallDataEx[0].InLine.GetEndPoint(0), lstWallDataEx[0].InLine.GetEndPoint(1), grid.Curve.GetEndPoint(0));
			XYZ xyz = grid.Curve.GetEndPoint(0) - this.GetFootPoint(lstWallDataEx[0].InLine.GetEndPoint(0), lstWallDataEx[0].InLine.GetEndPoint(1), grid.Curve.GetEndPoint(0));
			for (int i = 1; i < lstWallDataEx.Count; i++)
			{
				if (!this.m_DeleteWallId.Contains(lstWallDataEx[i].WallId.ToString()))
				{
					XYZ footPoint = this.GetFootPoint(lstWallDataEx[i].InLine.GetEndPoint(0), lstWallDataEx[i].InLine.GetEndPoint(1), grid.Curve.GetEndPoint(0));
					XYZ footPoint2 = this.GetFootPoint(lstWallDataEx[i].OutLine.GetEndPoint(0), lstWallDataEx[i].OutLine.GetEndPoint(1), grid.Curve.GetEndPoint(0));
					XYZ xyz2 = grid.Curve.GetEndPoint(0) - footPoint;
					double num3 = this.DistancePoint(grid.Curve.GetEndPoint(0), footPoint);
					double num4 = this.DistancePoint(grid.Curve.GetEndPoint(0), footPoint2);
					if (!xyz.Normalize().IsAlmostEqualTo(xyz2.Normalize(), 0.0001))
					{
						if (num2 < num3 || num2 < num4)
						{
							if (num3 > num4)
							{
								num2 = num3;
								anotherSide = (lstWallDataEx[i].InLine as Line);
							}
							else
							{
								num2 = num4;
								anotherSide = (lstWallDataEx[i].OutLine as Line);
							}
						}
					}
					else if (num < num3 || num < num4)
					{
						if (num3 > num4)
						{
							num = num3;
							oneSide = (lstWallDataEx[i].InLine as Line);
						}
						else
						{
							num = num4;
							oneSide = (lstWallDataEx[i].OutLine as Line);
						}
					}
				}
			}
			return !(oneSide != null) || !(anotherSide != null);
		}

		private List<WallDataEx> MergeWall(List<WallDataEx> lstWallDataEx)
		{
			List<Line> list = new List<Line>();
			List<WallDataEx> list2 = new List<WallDataEx>();
			for (int i = 0; i < lstWallDataEx.Count; i++)
			{
				if (!list.Contains(lstWallDataEx[i].InLine))
				{
					bool flag = false;
					for (int j = i + 1; j < lstWallDataEx.Count; j++)
					{
						if (!list.Contains(lstWallDataEx[j].InLine))
						{
                            Line combinationLine = YArchitech.Plumbing.Model.Algorithm.GetCombinationLine(lstWallDataEx[i].InLine as Line, lstWallDataEx[j].InLine as Line, 5.0);
							if (combinationLine == null)
							{
                                combinationLine = YArchitech.Plumbing.Model.Algorithm.GetCombinationLine(lstWallDataEx[i].InLine as Line, lstWallDataEx[j].OutLine as Line, 5.0);
							}
							if (combinationLine != null && this.WidthIsEqual(lstWallDataEx[i], lstWallDataEx[j]))
							{
								lstWallDataEx[i].InLine = combinationLine;
								list.Add(lstWallDataEx[i].InLine as Line);
								list.Add(lstWallDataEx[j].InLine as Line);
								if (!flag && (this.IsDimedWallDataEx(lstWallDataEx[i]) || this.IsDimedWallDataEx(lstWallDataEx[j])))
								{
									flag = true;
								}
							}
						}
					}
					if (!flag && !this.IsDimedWallDataEx(lstWallDataEx[i]))
					{
						list2.Add(lstWallDataEx[i]);
					}
				}
			}
			return list2;
		}

		private bool IsDimedWallDataEx(WallDataEx wallDataEx)
		{
			return this.m_LstDimensionedWall.Exists((ElementId w) => w.ToString() == wallDataEx.WallId.ToString());
		}

		private bool WidthIsEqual(WallDataEx wallDataExOne, WallDataEx wallDataExTwo)
		{
			return Math.Abs(wallDataExOne.WallWidth - wallDataExTwo.WallWidth) < 0.001;
		}

		private Grid FindNearestLine(Line wallLine, GridGroupDataEx gridGroupDataEx)
		{
			double num = -1.0;
			Grid result = null;
			foreach (GridInfoCache gridInfoCache in gridGroupDataEx.GridInfoCaches)
			{
				XYZ footPoint = this.GetFootPoint(wallLine.GetEndPoint(0), wallLine.GetEndPoint(1), gridInfoCache.ThisGridLine.GetEndPoint(0));
				double num2 = this.DistancePoint(gridInfoCache.ThisGridLine.GetEndPoint(0), footPoint);
                if ((num < 0.0 || num > num2) && (YJKArch.Utils.Geometry.Is_Point_OnSegment(gridInfoCache.ThisGridLine.GetEndPoint(0), gridInfoCache.ThisGridLine.GetEndPoint(1), this.GetFootPoint(gridInfoCache.ThisGridLine.GetEndPoint(0), gridInfoCache.ThisGridLine.GetEndPoint(1), wallLine.GetEndPoint(0)), 1E-09) || YJKArch.Utils.Geometry.Is_Point_OnSegment(gridInfoCache.ThisGridLine.GetEndPoint(0), gridInfoCache.ThisGridLine.GetEndPoint(1), this.GetFootPoint(gridInfoCache.ThisGridLine.GetEndPoint(0), gridInfoCache.ThisGridLine.GetEndPoint(1), wallLine.GetEndPoint(1)), 1E-09)))
				{
					num = num2;
					result = gridInfoCache.ThisGrid;
				}
			}
			return result;
		}

		private XYZ GetFootPoint(XYZ ptOne, XYZ ptTwo, XYZ ptThree)
		{
			double num = (ptOne.Y - ptTwo.Y) / (ptOne.X - ptTwo.X);
			double y = ptOne.Y;
			double y2 = ptOne.Y;
			double x = ptThree.X;
			double y3 = ptThree.Y;
			double num2;
			double num3;
			if (Math.Abs(ptOne.X - ptTwo.X) < 0.0001)
			{
				num2 = ptThree.Y;
				num3 = ptOne.X;
			}
			else if (Math.Abs(ptOne.Y - ptTwo.Y) < 0.0001)
			{
				num3 = ptThree.X;
				num2 = ptOne.Y;
			}
			else
			{
				double num4 = (ptTwo.Y - ptOne.Y) / (ptTwo.X - ptOne.X);
				num3 = (num4 * num4 * ptOne.X + num4 * (ptThree.Y - ptOne.Y) + ptThree.X) / (num4 * num4 + 1.0);
				num2 = num4 * (num3 - ptOne.X) + ptOne.Y;
			}
			return new XYZ(num3, num2, ptOne.Z);
		}

		private bool IsParallel(WallDataEx wallDataEx, GridGroupDataEx gridGroupDataEx)
		{
			return Math.Abs(wallDataEx.VecOutWall.X * gridGroupDataEx.VecGridNum.Y - wallDataEx.VecOutWall.Y * gridGroupDataEx.VecGridNum.X) < 0.0001;
		}

		private double DistancePoint(XYZ ptOne, XYZ ptTwo)
		{
			return Math.Abs(Math.Sqrt(Math.Pow(ptOne.X - ptTwo.X, 2.0) + Math.Pow(ptOne.Y - ptTwo.Y, 2.0)));
		}

		private BaseDataOper m_BaseDataOper;

		private Document m_Doc;

		private List<Dimension> m_LstDim = new List<Dimension>();

		private List<WallDataEx> m_LstAllWallDataEx = new List<WallDataEx>();

		private List<ElementId> m_LstDimensionedWall = new List<ElementId>();

		private List<WallDataEx> m_LstWallDataExFilter = new List<WallDataEx>();

		private List<string> m_DeleteWallId = new List<string>();

		private Dictionary<ElementId, ElementId> m_DicWallDim = new Dictionary<ElementId, ElementId>();

		private Dictionary<string, Grid> m_DicGrid = new Dictionary<string, Grid>();
	}
}
